package cz.muni.fi.pa165.business.serviceImpl;

import cz.muni.fi.pa165.api.PickerService;
import cz.muni.fi.pa165.api.dto.PickerDTO;
import cz.muni.fi.pa165.business.dao.PickerDAO;
import cz.muni.fi.pa165.business.service.utils.PickerObjectTransformer;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Marek Rebo
 */
@Service
public class PickerServiceImpl implements PickerService {

    @Autowired
    private PickerDAO pickerDAO;

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Transactional
    @Override
    public void createPicker(PickerDTO pickerDTO) {
        pickerDAO.createPicker(PickerObjectTransformer.pickerDTOToPicker(pickerDTO));
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    @Override
    public void updatePicker(PickerDTO pickerDTO) {
        pickerDAO.updatePicker(PickerObjectTransformer.pickerDTOToPicker(pickerDTO));
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    @Override
    public void deletePicker(PickerDTO pickerDTO) {
        pickerDAO.deletePicker(PickerObjectTransformer.pickerDTOToPicker(pickerDTO));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public PickerDTO getPicker(Long id) {
        return PickerObjectTransformer.pickerToPickerDTO(pickerDAO.getPicker(id));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<PickerDTO> findAllPickers() {
        return PickerObjectTransformer.pickerListToPickerDTOList(pickerDAO.findAllPickers());
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public PickerDTO findPickerByNickName(String nickName) {
        return PickerObjectTransformer.pickerToPickerDTO(pickerDAO.findPickerByNickName(nickName));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<PickerDTO> findPickersByFirstName(String firstName) {
        return PickerObjectTransformer.pickerListToPickerDTOList(pickerDAO.findPickersByFirstName(firstName));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<PickerDTO> findPickersByLastName(String lastName) {
        return PickerObjectTransformer.pickerListToPickerDTOList(pickerDAO.findPickersByLastName(lastName));
    }
}
